package o;

import com.google.android.exoplayer2.audio.AudioProcessor;
import com.google.android.exoplayer2.audio.AudioProcessor.UnhandledFormatException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public final class acq implements AudioProcessor {
    private byte[] ʻ = anr.ʽ;
    private boolean ʼ;
    private byte[] ʽ = anr.ʽ;
    private int ˊ;
    private int ˊॱ;
    private boolean ˋ;
    private boolean ˋॱ;
    private int ˎ = -1;
    private int ˏॱ;
    private int ͺ;
    private int ॱ = -1;
    private long ॱˊ;
    private ByteBuffer ॱॱ = ˏ;
    private ByteBuffer ᐝ = ˏ;

    public void ˎ(boolean z) {
        this.ˋ = z;
        ʼ();
    }

    public long ॱॱ() {
        return this.ॱˊ;
    }

    public boolean ˋ(int i, int i2, int i3) throws UnhandledFormatException {
        if (i3 != 2) {
            throw new UnhandledFormatException(i, i2, i3);
        } else if (this.ˎ == i && this.ॱ == i2) {
            return false;
        } else {
            this.ˎ = i;
            this.ॱ = i2;
            this.ˊ = i2 * 2;
            return true;
        }
    }

    public boolean ˏ() {
        return this.ˎ != -1 && this.ˋ;
    }

    public int ˋ() {
        return this.ॱ;
    }

    public int ˊ() {
        return 2;
    }

    public int ˎ() {
        return this.ˎ;
    }

    public void ˎ(ByteBuffer byteBuffer) {
        while (byteBuffer.hasRemaining() && !this.ॱॱ.hasRemaining()) {
            switch (this.ͺ) {
                case 0:
                    ˋ(byteBuffer);
                    break;
                case 1:
                    ˊ(byteBuffer);
                    break;
                case 2:
                    ॱ(byteBuffer);
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
    }

    public void ॱ() {
        this.ʼ = true;
        if (this.ˊॱ > 0) {
            ˋ(this.ʽ, this.ˊॱ);
        }
        if (!this.ˋॱ) {
            this.ॱˊ += (long) (this.ˏॱ / this.ˊ);
        }
    }

    public ByteBuffer ʽ() {
        ByteBuffer byteBuffer = this.ॱॱ;
        this.ॱॱ = ˏ;
        return byteBuffer;
    }

    public boolean ʻ() {
        return this.ʼ && this.ॱॱ == ˏ;
    }

    public void ʼ() {
        if (ˏ()) {
            int ˊ = ˊ(150000) * this.ˊ;
            if (this.ʽ.length != ˊ) {
                this.ʽ = new byte[ˊ];
            }
            this.ˏॱ = ˊ(20000) * this.ˊ;
            if (this.ʻ.length != this.ˏॱ) {
                this.ʻ = new byte[this.ˏॱ];
            }
        }
        this.ͺ = 0;
        this.ॱॱ = ˏ;
        this.ʼ = false;
        this.ॱˊ = 0;
        this.ˊॱ = 0;
        this.ˋॱ = false;
    }

    public void ᐝ() {
        this.ˋ = false;
        ʼ();
        this.ᐝ = ˏ;
        this.ॱ = -1;
        this.ˎ = -1;
        this.ˏॱ = 0;
        this.ʽ = anr.ʽ;
        this.ʻ = anr.ʽ;
    }

    private void ˋ(ByteBuffer byteBuffer) {
        int limit = byteBuffer.limit();
        byteBuffer.limit(Math.min(limit, byteBuffer.position() + this.ʽ.length));
        int ʻ = ʻ(byteBuffer);
        if (ʻ == byteBuffer.position()) {
            this.ͺ = 1;
        } else {
            byteBuffer.limit(ʻ);
            ˏ(byteBuffer);
        }
        byteBuffer.limit(limit);
    }

    private void ˊ(ByteBuffer byteBuffer) {
        int limit = byteBuffer.limit();
        int ᐝ = ᐝ(byteBuffer);
        int position = ᐝ - byteBuffer.position();
        int length = this.ʽ.length - this.ˊॱ;
        if (ᐝ >= limit || position >= length) {
            ᐝ = Math.min(position, length);
            byteBuffer.limit(byteBuffer.position() + ᐝ);
            byteBuffer.get(this.ʽ, this.ˊॱ, ᐝ);
            this.ˊॱ = ᐝ + this.ˊॱ;
            if (this.ˊॱ == this.ʽ.length) {
                if (this.ˋॱ) {
                    ˋ(this.ʽ, this.ˏॱ);
                    this.ॱˊ += (long) ((this.ˊॱ - (this.ˏॱ * 2)) / this.ˊ);
                } else {
                    this.ॱˊ += (long) ((this.ˊॱ - this.ˏॱ) / this.ˊ);
                }
                ˏ(byteBuffer, this.ʽ, this.ˊॱ);
                this.ˊॱ = 0;
                this.ͺ = 2;
            }
            byteBuffer.limit(limit);
            return;
        }
        ˋ(this.ʽ, this.ˊॱ);
        this.ˊॱ = 0;
        this.ͺ = 0;
    }

    private void ॱ(ByteBuffer byteBuffer) {
        int limit = byteBuffer.limit();
        int ᐝ = ᐝ(byteBuffer);
        byteBuffer.limit(ᐝ);
        this.ॱˊ += (long) (byteBuffer.remaining() / this.ˊ);
        ˏ(byteBuffer, this.ʻ, this.ˏॱ);
        if (ᐝ < limit) {
            ˋ(this.ʻ, this.ˏॱ);
            this.ͺ = 0;
            byteBuffer.limit(limit);
        }
    }

    private void ˋ(byte[] bArr, int i) {
        ˋ(i);
        this.ᐝ.put(bArr, 0, i);
        this.ᐝ.flip();
        this.ॱॱ = this.ᐝ;
    }

    private void ˏ(ByteBuffer byteBuffer) {
        ˋ(byteBuffer.remaining());
        this.ᐝ.put(byteBuffer);
        this.ᐝ.flip();
        this.ॱॱ = this.ᐝ;
    }

    private void ˋ(int i) {
        if (this.ᐝ.capacity() < i) {
            this.ᐝ = ByteBuffer.allocateDirect(i).order(ByteOrder.nativeOrder());
        } else {
            this.ᐝ.clear();
        }
        if (i > 0) {
            this.ˋॱ = true;
        }
    }

    private void ˏ(ByteBuffer byteBuffer, byte[] bArr, int i) {
        int min = Math.min(byteBuffer.remaining(), this.ˏॱ);
        int i2 = this.ˏॱ - min;
        System.arraycopy(bArr, i - i2, this.ʻ, 0, i2);
        byteBuffer.position(byteBuffer.limit() - min);
        byteBuffer.get(this.ʻ, i2, min);
    }

    private int ˊ(long j) {
        return (int) ((((long) this.ˎ) * j) / 1000000);
    }

    private int ᐝ(ByteBuffer byteBuffer) {
        for (int position = byteBuffer.position() + 1; position < byteBuffer.limit(); position += 2) {
            if (Math.abs(byteBuffer.get(position)) > 4) {
                return (position / this.ˊ) * this.ˊ;
            }
        }
        return byteBuffer.limit();
    }

    private int ʻ(ByteBuffer byteBuffer) {
        for (int limit = byteBuffer.limit() - 1; limit >= byteBuffer.position(); limit -= 2) {
            if (Math.abs(byteBuffer.get(limit)) > 4) {
                return ((limit / this.ˊ) * this.ˊ) + this.ˊ;
            }
        }
        return byteBuffer.position();
    }
}
